package client.tablete.workspace;


import java.io.IOException;
import java.util.concurrent.ExecutionException;

//import client.tablete.buttons.InformationButtonListener;
import client.tablete.buttons.*;

import websocket4j.client.WebSocket;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CommunicationActivity extends Activity{

	String answer;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.communication);
       
        answer = "";
        
        Bundle commBundle = this.getIntent().getExtras();
        String ip = commBundle.getString("ip");
        Log.i("ipCommunication", ip);
        String port = commBundle.getString("port");
        Log.i("portCommunication", port);
        TextView infoTv = (TextView) findViewById(R.id.textView2);
        infoTv.setText("ip: "+ip+" port: "+port);
        
//        Toast.makeText(CommunicationActivity.this, "ip: "+ip+" port: "+port, Toast.LENGTH_LONG).show();
        CommunicationThread tCom = new CommunicationThread(this);//(this) todo
        tCom.execute(ip,port);      
        
//        String message = null;
        try {
        	answer = tCom.get();
        } catch (InterruptedException e) {
        	e.printStackTrace();
        } catch (ExecutionException e) {
        	e.printStackTrace();
        }      

        if(answer!=null){
        	StateTv sTv = new StateTv(answer);
        	
        	ChannelButtonListener cBListener = new ChannelButtonListener(this, sTv.getChannel());
        	Button buttonChannel = (Button) findViewById(R.id.buttonChannel);
        	buttonChannel.setOnClickListener(cBListener);
        	
        	ProgrammationButtonListener pBListener = new ProgrammationButtonListener(this, sTv.getProgrammation());
        	Button buttonProg = (Button) findViewById(R.id.buttonProg);
        	buttonProg.setOnClickListener(pBListener);
        	
        	GenderButtonListener gBListener = new GenderButtonListener(this, sTv.getGender());
        	Button buttonGender = (Button) findViewById(R.id.buttonGender);
        	buttonGender.setOnClickListener(gBListener);
        	
        	TimeButtonListener tBListener = new TimeButtonListener(this, sTv.getStartTime(),sTv.getEndTime());
        	Button buttonTime = (Button) findViewById(R.id.buttonTime);
        	buttonTime.setOnClickListener(tBListener);
        	
        	InformationButtonListener iBListener = new InformationButtonListener(this, sTv.getDescription());
        	Button buttonInfo = (Button) findViewById(R.id.buttonInfo);
        	buttonInfo.setOnClickListener(iBListener);
        	
        	NavigatorButtonListener wBListener = new NavigatorButtonListener(this, sTv.getWebSite());
        	Button buttonNav = (Button) findViewById(R.id.buttonWww);
        	buttonNav.setOnClickListener(wBListener);
        }
    }
	
		
	public void showData(String data){
		if((data != null)&&(!data.equals("exit"))){
			TextView infoTv2 = (TextView) findViewById(R.id.textData);
			infoTv2.setText(data);    					
		}		
	}
	
	public void setAnswer(String answer){
		this.answer = answer;
	}
	
}


